package com.usabb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ifast.common.base.CoreServiceImpl;
import com.ifast.common.component.oss.support.UploadServer;
import com.ifast.common.utils.FileType;
import com.usabb.api.dto.AttachesDTO;
import com.usabb.dao.AttachDao;
import com.usabb.domain.AttachDO;
import com.usabb.enums.AttachImageTypeEnum;
import com.usabb.enums.BizTypeEnum;
import com.usabb.service.AttachService;
import com.usabb.utils.AttachUtil;
import com.usabb.utils.UploadFileNameUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@Slf4j
@Service
@AllArgsConstructor
public class AttachServiceImpl extends CoreServiceImpl<AttachDao, AttachDO> implements AttachService {

    private static final String PREFIX = "CENTER";
    private static final String PREFIX_HOSPITAL = "HOSPITAL";
    private static final String PREFIX_HOMESTAY = "HOMESTAY";

    private UploadServer uploader;

    @Override
    public List<AttachDO> getBy(BizTypeEnum bizTypeEnum, Long bizId) {
        AttachDO attachDTO = new AttachDO();
        attachDTO.setBizId(bizId);
        attachDTO.setBizType(bizTypeEnum.name());
        Wrapper<AttachDO> wrapper = new EntityWrapper<>(attachDTO);

        return this.selectList(wrapper);
    }

    @Override
    public AttachesDTO getAttachesDTOBy(BizTypeEnum bizTypeEnum, Long bizId) {
        final List<AttachDO> attachDOS = getBy(bizTypeEnum, bizId);

        final List<AttachDO> listTypeDOs = AttachUtil.filterListType(attachDOS);
        final List<AttachDO> realTypeDOs = AttachUtil.filterRealType(attachDOS);
        final List<AttachDO> avatarTypeDOs = AttachUtil.filterAvatarType(attachDOS);
        final List<AttachDO> wechatTypeDOs = AttachUtil.filterWechatType(attachDOS);
        List<String> list = AttachUtil.extractPath(listTypeDOs);
        List<String> real = AttachUtil.extractPath(realTypeDOs);
        List<String> avatar = AttachUtil.extractPath(avatarTypeDOs);
        List<String> wechat = AttachUtil.extractPath(wechatTypeDOs);

        return AttachesDTO.of(list, real, avatar, wechat);
    }

    @Override
    public void upload(AttachDO attachDO, MultipartFile file1) {
        log.info("input: attachDO:{}", attachDO);

        final String fileNameOnClient = file1.getOriginalFilename();
        final String fileName = UploadFileNameUtils.getFileName(fileNameOnClient, PREFIX);

        log.debug("fileNameOnClient:{}, fileName: {}", fileNameOnClient, fileName);
        String url = null;
        try {
            url = uploader.upload(file1.getBytes(), fileName);
        } catch (IOException e) {
            log.error("File upload IO Exception. bizId:{}, file:{}", attachDO.getBizId(), fileNameOnClient, e);
            return;
        }

        final String ext = FileType.getFileExt(fileNameOnClient); //扩展名
        attachDO.setPath(url);
        attachDO.setFileType(ext);
        attachDO.setBizType(PREFIX);

        this.insert(attachDO);

        log.info(file1.getOriginalFilename());
    }

    @Override
    public void upload2(AttachDO attachDO, MultipartFile file1) {
        log.info("input: attachDO:{}", attachDO);

        final String fileNameOnClient = file1.getOriginalFilename();
        final String fileName = UploadFileNameUtils.getFileName(fileNameOnClient, PREFIX_HOSPITAL);

        log.debug("fileNameOnClient:{}, fileName: {}", fileNameOnClient, fileName);
        String url = null;
        try {
            url = uploader.upload(file1.getBytes(), fileName);
        } catch (IOException e) {
            log.error("File upload IO Exception. bizId:{}, file:{}", attachDO.getBizId(), fileNameOnClient, e);
            return;
        }

        final String ext = FileType.getFileExt(fileNameOnClient); //扩展名
        attachDO.setPath(url);
        attachDO.setFileType(ext);
        attachDO.setBizType("PREFIX_HOSPITAL");

        this.insert(attachDO);

        log.info(file1.getOriginalFilename());
    }

    public void upload3(AttachDO attachDO, MultipartFile file1) {
        log.info("input: attachDO:{}", attachDO);

        final String fileNameOnClient = file1.getOriginalFilename();
        final String fileName = UploadFileNameUtils.getFileName(fileNameOnClient, PREFIX_HOMESTAY);

        log.debug("fileNameOnClient:{}, fileName: {}", fileNameOnClient, fileName);
        String url = null;
        try {
            url = uploader.upload(file1.getBytes(), fileName);
        } catch (IOException e) {
            log.error("File upload IO Exception. bizId:{}, file:{}", attachDO.getBizId(), fileNameOnClient, e);
            return;
        }

        final String ext = FileType.getFileExt(fileNameOnClient); //扩展名
        attachDO.setPath(url);
        attachDO.setFileType(ext);
        attachDO.setBizType("HOMESTAY");

        this.insert(attachDO);

        log.info(file1.getOriginalFilename());
    }

    private String uploadOne(MultipartFile file1) throws Exception {
        final String fileNameOnClient = file1.getOriginalFilename();
        final String fileName = UploadFileNameUtils.getFileName(fileNameOnClient, PREFIX);

        log.info("fileNameOnClient:{}, fileName: {}", fileNameOnClient, fileName);
        String url = null;
        try {
            url = uploader.upload(file1.getBytes(), fileName);
        } catch (IOException e) {
            log.error("File upload IO Exception. file:{}", fileNameOnClient, e);
            throw new Exception("File upload IO Exception.");
        }

        return url;
    }

    private void insertOneDo(AttachDO attachDO, String url, MultipartFile file1) {
        final String ext = FileType.getFileExt(file1.getOriginalFilename()); //扩展名

        AttachDO attach = new AttachDO();
        BeanUtils.copyProperties(attachDO, attach);

        attach.setPath(url);
        attach.setFileType(ext);
        attach.setImageType("LIST");
        attach.setBizType(PREFIX);

        this.insert(attach);
    }

    @Override
    public void upload(AttachDO attachDO, MultipartFile[] files) {
        for (MultipartFile file : files) {
            String url = StringUtils.EMPTY;
            try {
                url = uploadOne(file);
            } catch (Exception e) {
                log.error("File upload IO Exception. file:{}", file.getOriginalFilename(), e);
            }

            insertOneDo(attachDO, url, file);
        }
    }
    public void upload3(AttachDO attachDO, MultipartFile[] files) {
        for (MultipartFile file : files) {
            String url = StringUtils.EMPTY;
            try {
                url = uploadOne(file);
            } catch (Exception e) {
                log.error("File upload IO Exception. file:{}", file.getOriginalFilename(), e);
            }

            insertOneDo(attachDO, url, file);
        }
    }

}
